package cn.iocoder.yudao.module.enterprise.service.emissionbasis;

import cn.iocoder.yudao.module.enterprise.controller.admin.emissionbasis.vo.*;
import cn.iocoder.yudao.module.enterprise.convert.emissionbasis.BasisValueConvert;
import cn.iocoder.yudao.module.enterprise.convert.emissionbasis.EmissionBasisConvert;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.emissionbasis.BasisValueDO;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.emissionbasis.EmissionBasisDO;
import cn.iocoder.yudao.module.enterprise.dal.mysql.emissionbasis.BasisValueMapper;
import cn.iocoder.yudao.module.enterprise.dal.mysql.emissionbasis.EmissionBasisMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.enterprise.enums.ErrorCodeConstants.EMISSION_BASIS_HAS_EXISTS;
import static cn.iocoder.yudao.module.enterprise.enums.ErrorCodeConstants.EMISSION_BASIS_NOT_EXISTS;

/**
 * 排放基准 Service 实现类
 *
 * @author StevenShen
 */
@Service
@Validated
public class EmissionBasisServiceImpl implements EmissionBasisService {

    @Resource
    private EmissionBasisMapper emissionBasisMapper;
    @Resource
    private BasisValueMapper basisValueMapper;

    @Override
    public Long createEmissionBasis(EmissionBasisCreateReqVO createReqVO) {
        // 校验该类型的排放基准是否已经存在
        EmissionBasisDO emissionBasisDO = emissionBasisMapper.selectOneByCategory(createReqVO.getCategory());
        if (emissionBasisDO != null) {
            throw exception(EMISSION_BASIS_HAS_EXISTS);
        }

        // 插入排放基准表
        EmissionBasisDO emissionBasis = EmissionBasisConvert.INSTANCE.convert(createReqVO);
        emissionBasisMapper.insert(emissionBasis);

        // 插入基准排放量表
        List<BasisValueCreateReqVO> basisValueList = createReqVO.getBasisValueList();
        List<BasisValueDO> basisValueDOList = BasisValueConvert.INSTANCE.convertList(basisValueList);
        basisValueDOList = basisValueDOList.stream()
                .map(e -> e.setBasisId(emissionBasis.getId()))
                .collect(Collectors.toList());
        basisValueMapper.insertBatch(basisValueDOList);

        // 返回
        return emissionBasis.getId();
    }

    @Override
    public void updateEmissionBasis(EmissionBasisUpdateReqVO updateReqVO) {
        Long basisId = updateReqVO.getId();

        // 校验存在
        validateEmissionBasisExists(basisId);

        // 更新排放基准表
        EmissionBasisDO updateObj = EmissionBasisConvert.INSTANCE.convert(updateReqVO);
        emissionBasisMapper.updateById(updateObj);

        // 更新基准排放量表
        basisValueMapper.deleteByBasisId(basisId);
        List<BasisValueUpdateReqVO> basisValueList = updateReqVO.getBasisValueList();
        List<BasisValueDO> basisValueDOList = BasisValueConvert.INSTANCE.convertList03(basisValueList);
        basisValueDOList = basisValueDOList.stream()
                .map(e -> e.setBasisId(updateReqVO.getId())
                        .setId(null))
                .collect(Collectors.toList());
        basisValueMapper.insertBatch(basisValueDOList);
    }

    @Override
    public void deleteEmissionBasis(Long id) {
        // 校验存在
        validateEmissionBasisExists(id);
        // 删除
        emissionBasisMapper.deleteById(id);
    }

    private void validateEmissionBasisExists(Long id) {
        if (emissionBasisMapper.selectById(id) == null) {
            throw exception(EMISSION_BASIS_NOT_EXISTS);
        }
    }

    @Override
    public EmissionBasisRespVO getEmissionBasis(Long id) {
        EmissionBasisDO emissionBasisDO = emissionBasisMapper.selectById(id);
        EmissionBasisRespVO respVO = EmissionBasisConvert.INSTANCE.convert(emissionBasisDO);

        // 查询基准排放量列表
        List<BasisValueDO> basisValueDOList = basisValueMapper.selectListByBasisId(id);
        respVO.setBasisValueList(BasisValueConvert.INSTANCE.convertList02(basisValueDOList));

        return respVO;
    }

    @Override
    public List<EmissionBasisRespVO> getEmissionBasisList() {
        List<EmissionBasisDO> emissionBasisList = emissionBasisMapper.selectList();

        return emissionBasisList.stream().map(emissionBasisDO -> {
            EmissionBasisRespVO respVO = EmissionBasisConvert.INSTANCE.convert(emissionBasisDO);
            // 查询基准排放量列表
            List<BasisValueDO> basisValueDOList = basisValueMapper.selectListByBasisId(emissionBasisDO.getId());
            respVO.setBasisValueList(BasisValueConvert.INSTANCE.convertList02(basisValueDOList));
            return respVO;
        }).collect(Collectors.toList());
    }

    @Override
    public EmissionBasisDO getEmissionBasisByCategory(Integer category) {
        return emissionBasisMapper.selectOneByCategory(category);
    }

}
